#!/usr/bin/env python3
# coding:utf8
import csv
import unittest
import time
import os
from basis.basis_driver import BasisBrowser, BasisDriver
from config.text_config import open_tkinter, open_turtle
from pages.login_page import LoginPage
from pages.order_page import OrderPage
from pages.search_page import SearchPage


class WebTestLogin(unittest.TestCase,LoginPage):

    Adress = '192.168.1.195'
    BASIS_BROWSER = BasisBrowser.Chrome
    path = os.path.abspath(os.path.dirname(os.getcwd()))
    '''使用basis_driver'''
    def setUp(self):
        # self.URL = "http://" + self.Adress + ":8081/bi"
        self.URL = "http://www.shunyingglobal.cn/Account/Login"
        self.driver = BasisDriver(self.BASIS_BROWSER)
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.base_url = self.URL
        self.LOGINPAGE = LoginPage(self.driver,self.base_url)   #实例化LoginPage作为局部变量时使用
        self.LOGINPAGE.open("")
        # open_tkinter("/data/huozhan.gif",
        #                          '\n -----------模拟用户搜索商品-----------\n',
        #                          '''
        # 1、系统用户下单单个商品
        # 2、系统用户单购物车(进货单下单多个供应商的多个商品
        # 3、系统用户下单关注商品（包含关注逻辑）
        #                          ''')
        LOGINPAGE = LoginPage(self.driver, self.base_url)
        LOGINPAGE.login('15020579521', '123456')
        self.driver.sleep(2)

    def tearDown(self):
        self.driver.quit_browser()
        pass

    def test_1orser(self):
        open_turtle("系统用户下单单个商品", x=-150, y=50)
        self.log("打开csv文件")
        self.driver.xlsx_csv("/data/case.xlsx", "comdisysno", "/data/csv/comdisysno.csv")
        self.csv_file = open(self.path + r'/data/csv/comdisysno.csv',
                             mode='r',
                             encoding='utf8')
        csv_data = csv.reader(self.csv_file)
        is_header = True
        for line, i in zip(csv_data, range(2)):
            if is_header:
                is_header = False
                continue
            data = {'number': line[0],

                    'User_name': line[4],
                    'Mobile_phone': line[5],
                    'IDcar': line[6],
                    'province': line[7],
                    'city': line[8],
                    'area': line[9],
                    'Detailed_address': line[10]}
            search = OrderPage(self.driver,self.base_url)
            search.order(data)
            self.log("循环%d次" % i)
        self.csv_file.close()

    def test_3orders_Suppliers(self):
        open_turtle("系统用户购物车下单多个商品", x=-150, y=50)
        self.log("打开csv文件")
        self.driver.xlsx_csv("/data/case.xlsx", "comdisysno", "/data/csv/comdisysno.csv")
        self.csv_file = open(self.path + r'/data/csv/comdisysno.csv',
                             mode='r',
                             encoding='utf8')
        csv_data = csv.reader(self.csv_file)
        is_header = True
        for line,i in zip(csv_data,range(2)):
            if is_header:
                is_header = False
                continue
            data = {'number': line[0],

                    'User_name': line[4],
                    'Mobile_phone':line[5],
                    'IDcar': line[6],
                    'province':line[7],
                    'city':line[8],
                    'area':line[9],
                    'Detailed_address':line[10]}
            search = OrderPage(self.driver,self.base_url)
            search.orders_Suppliers_ShoppingCar(data)
            self.log("循环%d次"%i)

        self.csv_file.close()

    def test_4orders_PayCloseAttentionTo(self):
        open_turtle("系统用户下单关注商品", x=-150, y=50)
        self.driver.xlsx_csv("/data/case.xlsx", "comdisysno", "/data/csv/comdisysno.csv")
        self.csv_file = open(self.path + r'/data/csv/comdisysno.csv',
                             mode='r',
                             encoding='utf8')
        csv_data = csv.reader(self.csv_file)
        is_header = True
        for line, i in zip(csv_data, range(2)):
            if is_header:
                is_header = False
                continue
            data = {'number': line[0],

                    'User_name': line[4],
                    'Mobile_phone': line[5],
                    'IDcar': line[6],
                    'province': line[7],
                    'city': line[8],
                    'area': line[9],
                    'Detailed_address': line[10]}
            search = OrderPage(self.driver, self.base_url)
            search.orders_PayCloseAttentionTo(data)
            self.log("循环%d次" % i)
        self.csv_file.close()

if __name__ == '__main__':
    unittest.main()
